package com.atguigu.tingshu.user.strategy.impl;

import com.atguigu.tingshu.album.client.TrackInfoFeignClient;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.model.album.TrackInfo;
import com.atguigu.tingshu.model.user.UserPaidTrack;
import com.atguigu.tingshu.user.mapper.UserPaidTrackMapper;
import com.atguigu.tingshu.user.strategy.PaymentStrategy;
import com.atguigu.tingshu.vo.user.UserPaidRecordVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author atguigu-mqx
 * @ClassName TrackPaymentStrategy
 * @description: TODO
 * @date 2024年06月26日
 * @version: 1.0
 */
@Service("1002")
public class TrackPaymentStrategy implements PaymentStrategy {

    @Autowired
    private UserPaidTrackMapper userPaidTrackMapper;

    @Autowired
    private TrackInfoFeignClient trackInfoFeignClient;

    /**
     * 购买声音记录
     * @param userPaidRecordVo
     */
    @Override
    public void processPayment(UserPaidRecordVo userPaidRecordVo) {
        //  购买声音
        Long count = userPaidTrackMapper.selectCount(new LambdaQueryWrapper<UserPaidTrack>().eq(UserPaidTrack::getOrderNo, userPaidRecordVo.getOrderNo()));
        if (count != 0) return;
        //  通过声音Id声音对象;
        Long trackId = userPaidRecordVo.getItemIdList().get(0);
        //  找声音对象。
        Result<TrackInfo> trackInfoResult = trackInfoFeignClient.getTrackInfoById(trackId);
        TrackInfo trackInfo = trackInfoResult.getData();
        //  保存数据
        userPaidRecordVo.getItemIdList().forEach(itemId -> {
            UserPaidTrack userPaidTrack = new UserPaidTrack();
            userPaidTrack.setOrderNo(userPaidRecordVo.getOrderNo());
            userPaidTrack.setUserId(userPaidRecordVo.getUserId());
            userPaidTrack.setAlbumId(trackInfo.getAlbumId());
            //  声音Id 对应的应该是一个集合.
            userPaidTrack.setTrackId(itemId);
            //  保存数据
            userPaidTrackMapper.insert(userPaidTrack);
        });
    }
}
